﻿state = 0   -- line readed from .debug th output
            -- 0 Syntax: | other sys msg
            -- 1 Threat list of ....
            -- 2 Nr. Nick (guid xxx)  - threat xx.xxxx
            
name = UnitName("player");
work = 0 --working
auto = 0 --autoupdate
visible = 0
playerscount = 0
updatepos = 1
target = ""
playersnicks = {"Player 1", "Player 2", "Player 3", "Player 4"}
playersthreat = {0, 0, 0, 0}

--http://wowprogramming.com/snippets/Create_a_mini-timer_using_OnUpdate_3
total = 0
function onUpdate(self,elapsed)
    total = total + elapsed
    if total >= 10 then
		if auto == 1 then
			SendChatMessage(".debug threatlist")
		end
        total = 0
    end
end
 
f = CreateFrame("frame")
f:SetScript("OnUpdate", onUpdate)
-- ==========================================================================
function Check_OnClick()
	SendChatMessage(".debug threatlist")
end

function Turn_OnClick()
	if work == 0 then
		work = 1
		ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", FilterSysMessage)
		coreframe:RegisterEvent("PLAYER_REGEN_DISABLED")
		coreframe:RegisterEvent("PLAYER_REGEN_ENABLED")
		State:SetText("On")
	else
		work = 0
		ChatFrame_RemoveMessageEventFilter("CHAT_MSG_SYSTEM", FilterSysMessage)
		coreframe:UnregisterEvent("PLAYER_REGEN_DISABLED")
		coreframe:UnregisterEvent("PLAYER_REGEN_ENABLED")
		State:SetText("Off")
	end
end

function TurnOn()
    auto = 1
end

function TurnOff()
	auto = 0
	ResetList()
	UpdateChart()
end

coreframe = CreateFrame("FRAME", "DTLCoreFrame")

function CoreFrameEvent(self, event, ...)
	if event == "PLAYER_REGEN_DISABLED" then
		TurnOn()
	end
	if event == "PLAYER_REGEN_ENABLED" then
		TurnOff()
	end
end

coreframe:SetScript("OnEvent", CoreFrameEvent)

function UpdateTarget(tar)
	target = tar
end

function UpdateList(position, nick, threat)
    playersnicks[position] = nick
    playersthreat[position] = math.floor(threat)
end

function ResetList()
	target = "Target"

	playersthreat[1] = 0
	playersthreat[2] = 0
	playersthreat[3] = 0
	playersthreat[4] = 0

	playersnicks[1] = "Player 1"
	playersnicks[2] = "Player 2"
	playersnicks[3] = "Player 3"
	playersnicks[4] = "Player 4"
end

function UpdateChart()
	Target1:SetText(target)
    
	Position1:SetText(playersnicks[1])
	Threat1:SetText(playersthreat[1])
	Texture1:SetWidth(150)
	
	Position2:SetText(playersnicks[2])
	Threat2:SetText(playersthreat[2])
	if playersthreat[1]>0 then
		Texture2:SetWidth(math.floor(150*(playersthreat[2]/playersthreat[1])))
	else
		Texture2:SetWidth(150)
	end
	
	Position3:SetText(playersnicks[3])
	Threat3:SetText(playersthreat[3])
	if playersthreat[1]>0 then
		Texture3:SetWidth(math.floor(150*(playersthreat[3]/playersthreat[1])))
	else
		Texture3:SetWidth(150)
	end
	
	Position4:SetText(playersnicks[4])
	Threat4:SetText(playersthreat[4])
	if playersthreat[1]>0 then
		Texture4:SetWidth(math.floor(150*(playersthreat[4]/playersthreat[1])))
	else
		Texture4:SetWidth(150)
	end
	
	if playersnicks[1] == name then
		Position1:SetTextColor(192,0,0)
	else
		Position1:SetTextColor(255,210,0)
	end
	
	if playersnicks[2] == name then
		Position2:SetTextColor(192,0,0)
	else
		Position2:SetTextColor(255,210,0)
	end
	
	if playersnicks[3] == name then
		Position3:SetTextColor(192,0,0)
	else
		Position3:SetTextColor(255,210,0)
	end

	if playersnicks[4] == name then
		Position4:SetTextColor(192,0,0)
	else
		Position4:SetTextColor(255,210,0)
	end
	
end

function FilterSysMessage(self)
    local msg = self
    local target
    local nick
    local threat
    
    if state == 0 then
        if string.find(msg, "Threat list of") ~= nil then
			UpdateTarget(strsub(msg, 16, string.find(msg, "(guid", 16, 1) -1))
            state = 1
        end
    elseif state == 1 then
        if string.find(msg, "End of threat") ~= nil then
            state = 0
			playerscount = updatepos-1
            UpdateChart()
			ResetList()
			playerscount = 0
			updatepos = 1
        else
            nick = strsub(msg, 9, string.find(msg, "%s", 9)-1)
            threat = strsub(msg, string.find(msg, "threat")+7)
            UpdateList(updatepos, nick, tonumber(threat))
            updatepos = updatepos + 1
            if updatepos>4 then
                updatepos = 1
            end
        end
    end
    return true
end

SLASH_DTL1 = "/dtl"
SlashCmdList["DTL"] = function()
	if visible == 1 then
		visible = 0
		Frame1:Hide()
	else 
		visible = 1
		Frame1:Show()
	end
end

--add
-- not in combat = state -> off + reset chart reset list
-- combat = state->on
-- force check
-- turn on / off

--global counter which line is actual
--Threat list of Praline Stalker (guid 25578)
-- 1. Sellbpuha (guid 326392) - threat 31.359997
--End of threat list.

